package com.blast.comicsisland.model.myview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.ScrollView;

import com.blast.comicsisland.model.community.Info;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Android Studio
 * Author: hg
 * Date: 15/7/1
 * Email: 531057892@qq.com
 */
public class PuBuLu extends ScrollView {

    private int viewCount;
    private View headView;
    private Info info;
    private int lastItem = -1;
    //第一级的LinearLayout
    private LinearLayout waterPool;

    //第二季的LinearLayout
    private List<LinearLayout> columns;

    private int columnNum = 2;

    //
    private OnLoadMoreListener onLoadMoreListener;

    private OnItemOnClick onItemOnClick;

    public PuBuLu(Context context) {
        super(context);
        init(context, null);
    }

    public PuBuLu(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }

    public void setOnClickItem(OnItemOnClick onItemOnClick) {
        this.onItemOnClick = onItemOnClick;
    }

    private void init(Context context, AttributeSet attrs) {
        if (headView != null) {
            LinearLayout bigWaterPool = new LinearLayout(context);
            bigWaterPool.setOrientation(LinearLayout.VERTICAL);

            ViewGroup.LayoutParams lp =
                    new RadioGroup.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT
                    );
            bigWaterPool.setLayoutParams(lp);
            this.addView(bigWaterPool);
            bigWaterPool.addView(headView);
            instancePuBuLu(context, bigWaterPool);
        } else {
            instancePuBuLu(context, null);
        }
    }

    private void instancePuBuLu(Context context, LinearLayout bigWaterPool) {
        columns = new LinkedList<LinearLayout>();

        waterPool = new LinearLayout(context);
        waterPool.setOrientation(LinearLayout.HORIZONTAL);

        ViewGroup.LayoutParams lp =
                new RadioGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT
                );

        waterPool.setLayoutParams(lp);
        if (bigWaterPool != null) {
            bigWaterPool.addView(waterPool);
        } else {
            this.addView(waterPool);
        }

        for (int i = 0; i < columnNum; i++) {
            LinearLayout linearLayout = new LinearLayout(context);
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    0,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    1
            );
            linearLayout.setPadding(2,2,2,2);
            linearLayout.setLayoutParams(params);
            waterPool.addView(linearLayout);
            columns.add(linearLayout);
        }

    }


    public void addPuBuLuView(View view, final List<Info> infos,int position) {
        if (view != null) {
            if (lastItem < position) {
                view.setTag(viewCount);
                lastItem = position;
                int index = viewCount % columns.size();
                viewCount++;
                LinearLayout linearLayout = columns.get(index);

                linearLayout.addView(view);
                int childCount = linearLayout.getChildCount();
                Log.d("PuBuLu", "linearLayoutChild:" + childCount);

                view.setOnTouchListener(new OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        boolean ret = false;
                        int action = event.getAction();
                        switch (action) {
                            case MotionEvent.ACTION_DOWN:
                                ret = true;
                                break;
                            case MotionEvent.ACTION_UP:
                                if (onItemOnClick != null) {
                                    int itemPosition = (int) v.getTag();
                                    info = infos.get(itemPosition);
                                    onItemOnClick.itemOnClick(info, itemPosition - 1);
                                    Log.d("PuBuLu", "item:" + itemPosition);
                                }
                                break;
                        }

                        return ret;
                    }
                });

                view = null;
            }
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_UP:
                //内容的高度
                int height = waterPool.getHeight();
                //屏幕的高度
                int scrollViewHeight = getHeight();
                //滑出界面的高度
                int scrollY = getScrollY();
                if (scrollY + scrollViewHeight >= height) {
                    if (onLoadMoreListener != null) {
                        onLoadMoreListener.onBottom();
                    }
                }
                break;
        }
        return super.onTouchEvent(ev);
    }

    public void addHeadView(View headView) {
        this.removeAllViews();
        this.headView = headView;
        init(getContext(), null);
    }

    public interface OnLoadMoreListener {

        public void onBottom();

    }

    public interface OnItemOnClick {
        public void itemOnClick(Info info, int position);
    }

}
